package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.configure.config.AliyunOSSConfig;
import com.itheima.constant.MessageConstant;
import com.itheima.controller.dto.SetMealDto;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.BizException;
import com.itheima.mapper.SetMealCheckGroupMapper;
import com.itheima.mapper.SetMealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealCheckgroup;
import com.itheima.service.SetMealService;
import com.itheima.util.AliyunOSSUtil;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

/**
 * @Description 套餐服务类接口实现类
 * @Author makun
 * @Date 2023/9/14 15:41
 * @Version 1.0
 */
@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetMealService {
    @Autowired
    private SetMealCheckGroupMapper setMealCheckGroupMapper;
    @Autowired
    private AliyunOSSUtil aliyunOssUtil;
    @Autowired
    private AliyunOSSConfig aliyunOSSConfig;

    @Override
    public Page<Setmeal> findPage(QueryPageBean queryPageBean) {
        // 查询条件
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        if (!StringUtils.isNullOrEmpty(queryString)) {
            setmealLambdaQueryWrapper.like(Setmeal::getCode, queryString)
                    .or()
                    .like(Setmeal::getName, queryString)
                    .or()
                    .like(Setmeal::getHelpCode, queryString);
        }
        // 分页条件
        Page<Setmeal> setmealPage = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        // 执行查询
        baseMapper.selectPage(setmealPage, setmealLambdaQueryWrapper);
        return setmealPage;
    }

    @Override
    public Result findById(Integer id) {
        Setmeal setmeal = baseMapper.selectById(id);
        boolean flag = !Objects.isNull(setmeal);
        return new Result(flag, flag ? MessageConstant.QUERY_SETMEAL_SUCCESS : MessageConstant.QUERY_SETMEAL_FAIL, setmeal);
    }

    @Override
    public Result edit(Integer[] checkgroupIds, Setmeal setmeal) {
        // 查询原来的套餐信息
        Setmeal preSetMeal = baseMapper.selectById(setmeal.getId());
        // 如果图片为新图片则删除旧图片
        if (!setmeal.getImg().equals(preSetMeal.getImg())) {
            aliyunOssUtil.delete(aliyunOSSConfig.getPathSetMeal(), aliyunOssUtil.getFileNameByURL(preSetMeal.getImg()));
        }
        // 清空多表关系
        setMealCheckGroupMapper.delete(new LambdaQueryWrapper<SetmealCheckgroup>()
                .eq(SetmealCheckgroup::getSetmealId, setmeal.getId()));
        // 添加多表关系
        for (Integer checkGroupId : checkgroupIds) {
            setMealCheckGroupMapper.insert(new SetmealCheckgroup(setmeal.getId(), checkGroupId));
        }
        // 更新基础信息
        boolean flag = baseMapper.updateById(setmeal) == 1;
        return new Result(flag, flag ? MessageConstant.EDIT_SETMEAL_SUCCESS : MessageConstant.EDIT_SETMEAL_FAIL);
    }

    @Override
    public Result upload(MultipartFile imgFile) {
        String upload = aliyunOssUtil.upload(imgFile, aliyunOSSConfig.getPathSetMeal());
        return new Result(true, MessageConstant.UPLOAD_SUCCESS, upload);
    }

    @Override
    public Result add(Integer[] checkGroupIds, Setmeal setmeal) {
        // 添加套餐基本信息
        baseMapper.insert(setmeal);
        // 添加套餐与检查组的关系
        for (Integer checkGroupId : checkGroupIds) {
            setMealCheckGroupMapper.insert(new SetmealCheckgroup(setmeal.getId(), checkGroupId));
        }

        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    @Override
    public Result delete(Integer setMealId) {
        // 删除图片
        aliyunOssUtil.delete(aliyunOSSConfig.getPathSetMeal(), aliyunOssUtil.getFileNameByURL(baseMapper.selectById(setMealId).getImg()));
        // 先清空套餐与检查组的关系
        setMealCheckGroupMapper.delete(new LambdaQueryWrapper<SetmealCheckgroup>()
                .eq(SetmealCheckgroup::getSetmealId, setMealId));
        // 删除套餐
        boolean flag = baseMapper.deleteById(setMealId) == 1;
        return new Result(flag, flag ? MessageConstant.DELETE_SETMEAL_SUCCESS : MessageConstant.DELETE_SETMEAL_FAIL);
    }

    @Override
    public boolean saveOne(SetMealDto setMealDto) {
        // 添加图片
        String imageUrl = aliyunOssUtil.upload(setMealDto.getImageFile(), aliyunOSSConfig.getPathSetMeal());
        setMealDto.setImg(imageUrl);
        // 添加套餐基本信息
        baseMapper.insert(setMealDto);
        // 添加套餐与检查组的关系
        for (Integer checkGroupId : setMealDto.getCheckGroupIds()) {
            setMealCheckGroupMapper.insert(new SetmealCheckgroup(setMealDto.getId(), checkGroupId));
        }

        return true;
    }
}
